#include "common_opr_all.h"
#include <chrono>
#include <unistd.h>
#include <sys/stat.h>

CommonOprSystem::CommonOprSystem()
{
}

CommonOprSystem::~CommonOprSystem()
{
}

std::string CommonOprSystem::getPwd()
{
    char buf[1024] = {};
    getcwd(buf, sizeof(buf));
    return std::string(buf) + "/";
}


std::string CommonOprSystem::getFileSize(const std::string &fileName)
{
    struct stat buf;
    memset(&buf, 0, sizeof(buf));
    int retCI = stat((char *)fileName.c_str(), &buf);
    (void)retCI;
    auto ret = std::to_string(buf.st_size);
    return ret;
}

off_t CommonOprSystem::getFileSizeDigit(const std::string &fileName)
{
    struct stat buf;
    memset(&buf, 0, sizeof(buf));
    int retCI = stat((char *)fileName.c_str(), &buf);
    (void)retCI;
    off_t ret = buf.st_size;
    return ret;
}

std::string CommonOprSystem::getFileBaseName(const std::string &fileName)
{
    char *baseIndex = NULL;
    std::string ret;
    
    try
    {
        baseIndex = basename((char *)fileName.c_str());
    }
    catch (const std::exception &e)
    {
        Log_ERROR("getFileBaseName;trow exception: {}", e.what());
    }
    
    if (baseIndex != NULL)
    {
        ret = std::string(baseIndex);
    }

    return ret;
}

int CommonOprSystem::readFileAllOnce(const string &dstFile, string &readOutBuffStr)
{
    filebuf *pbuf;
    ifstream filestr;
    long fileSize;

    // 要读入整个文件，必须采用二进制打开 
    filestr.open(dstFile, ios::binary);
	if (!filestr.is_open())
	{
		Log_Info("[CommonOprSystem] [readFileAllOnce] open dstFile failed! dstFile:{}", dstFile);
		return DC_FAIL;
	}

    // 获取filestr对应buffer对象的指针 
    pbuf = filestr.rdbuf();

    // 调用buffer对象方法获取文件大小
    fileSize = pbuf->pubseekoff (0,ios::end, ios::in);
    pbuf->pubseekpos(0,ios::in);

    // 分配内存空间
    readOutBuffStr.resize(fileSize);

    // 获取文件内容
    pbuf->sgetn((char *)readOutBuffStr.c_str(), fileSize);
    filestr.close();
    return DC_SUCC;
}

// 判断文件是否存在
bool CommonOprSystem::isPathExist(const string &path) 
{
    if (access(path.c_str(), F_OK) == 0)
    {
        return true;
    }
    return false;
}
// 查询文件的大小
void CommonOprSystem::queryFileSize(const string& filePath, int &FileSize)
{
    FileSize = 0;
    if (filePath.empty() || !isPathExist(filePath))
    {
        return;
    }

    struct stat filestat;
    memset(&filestat, 0, sizeof(filestat));
    int ret=stat((char *)filePath.c_str(), &filestat);
    (void)ret;
    FileSize = (int)filestat.st_size;
}
int CommonOprSystem::readFilePartly(const string &dstFile, int offset, int size, string &readOutBuffStr)
{
    filebuf *pbuf;
    ifstream filestr;
    long fileSize;
    if (0 == size)
    {
        return DC_SUCC;
    }

    // 采用二进制打开 
    filestr.open(dstFile, ios::binary);
    if (!filestr.is_open())
    {
        Log_Info("[CommonOprSystem] [readFilePartly] open dstFile failed! dstFile:{}", dstFile);
        return DC_FAIL;
    }

    // 获取filestr对应buffer对象的指针 
    pbuf = filestr.rdbuf();

    // 调用buffer对象方法获取文件大小
    fileSize = pbuf->pubseekoff(0,ios::end, ios::in);
    if (fileSize < offset + size)
    {
        Log_Info("[CommonOprSystem] [readFilePartly] read dstFile failed! dstFile:{}", dstFile);
        filestr.close();
        return DC_FAIL;
    }

    pbuf->pubseekpos(offset, ios::in);
    // 分配内存空间
    readOutBuffStr.resize(size);
    // 获取文件内容
    pbuf->sgetn((char *)readOutBuffStr.c_str(), size);
    filestr.close();
    return DC_SUCC;
}

int CommonOprSystem::writeStringBuffToFile(string dstFile, string &stringBuff)
{
	ofstream desFile(dstFile, std::ios::out | std::ios::ate);
	if (!desFile.is_open())
	{
        Log_Info("[CommonOprSystem] [writeStringBuffToFile] open dstFile failed! dstFile:{}", dstFile);
		return DC_FAIL;
	}
	desFile << stringBuff;
	desFile.close();
    Log_Info("[CommonOprSystem] [writeStringBuffToFile] write StringBuff To File succ, dstFile:{}", dstFile);
    return DC_SUCC;
}

void CommonOprSystem::dealTargetFile(const string& filePath)
{
    if (!isPathExist(filePath)) 
    {
        return;
    }
    Log_Info("[CommonOprSystem] [dealTargetFile], filePath: {}", filePath);
    int ret = remove(filePath.c_str());
    (void)ret;
}















